<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Model\Order;
use App\Model\Customer;
use App\Model\Deal;
use App\Model\Route;
use App\Model\Biaoqian;
use App\Model\Hetongtype;
use App\Model\Hetong;
use App\Model\Gongyingshang;
use App\Model\Supplier;
use App\Model\Gendan;
use App\Model\Receive; 
use App\Model\Funds;
class ChengjiaoController extends Controller
{
    // 调用工单详情页面
    public function index($id)
    { 
        
    	$order=Order::where('id',$id)->first();
    	$customer=Customer::where('id',$order->order_customerid)->first();
    	$laiyuan=['1'=>'网站','2'=>'转介绍','3'=>'上门','4'=>'电话销售','5'=>'老客户','6'=>'其他'];
    	$xingbie=['1'=>'男','2'=>'女'];
    	$customer->customer_sex=$xingbie[$customer->customer_sex];  //客户性别
    	$customer->customer_laiyuan=$laiyuan[$customer->customer_laiyuan]; //客户来源
    	$deal=Deal::where('deal_orderid',$id)->first();
    	$type=Route::where('parent_id',0)->get();
    	$biaoqian=Biaoqian::all();
    	$order_didian=Route::where('id',$order->order_didian)->first()->title;
    	$order->order_didian=$order_didian;         //旅游类型
    	$didian=Route::where('id',$order->order_didian2)->first()->title;
    	$order->order_didian2=$didian;			//旅游地点
    	$order_lvyouleibie=Biaoqian::where('id',$order->order_lvyouleibie)->first()->name;
    	$order->order_lvyouleibie=$order_lvyouleibie;	//旅游标签
        $statue=['1'=>'商谈','3'=>'失败','4'=>'成功'];
        $order->order_statue=$statue[$order->order_statue];
        $order->staff_id=\DB::table('admin_users')->where('id',$order->staff_id)->first()->username;
    	if($deal){
            $deal=Deal::where('deal_orderid',$id)->first();
            $supplier=\DB::table('crm_deal_crm_supplier')->where('crm_deal_id',$deal->id)->pluck('crm_supplier_id');
            $supplier_logoname=Supplier::whereIn('id',$supplier)->pluck('supplier_logoname','id');
            $aa="";
            foreach ($supplier_logoname as $key => $value) {
                $aa.=$value.',';
            }
            $deal->deal_gongyingshang=$aa;
            $shoukuan=Receive::where('receive_dealid',$deal->id)->get();
            $qingkuan=Funds::where('funds_dealid',$deal->id)->get();
            $youke=\DB::table('deal_tourist')->where('deal_id',$deal->id)->pluck('tourist_id');
            $youke=\DB::table('crm_tourist')->whereIn('id',$youke)->get();
    		return view('deal.index',['order'=>$order,'customer'=>$customer,'deal'=>$deal,'shoukuan'=>$shoukuan,'funds'=>$qingkuan,'youke'=>$youke]);                                                                                                                                 
    	}else{
    		return view('order.xiangqing',['order'=>$order,'customer'=>$customer,'type'=>$type,'biaoqian'=>$biaoqian]);
    	}
    }
    // 执行工单修改
    public function xiugai(Request $request)
    {       
        $list=json_decode($request['data']);
        $data=[];
        $data['order_didian']=Route::where('title',$list->{'order_didian'})->first()->id;
        $data['order_didian2']=Route::where('title',$list->{'order_didian2'})->first()->id;
        $data['order_xianlu']=$list->{'order_xianlu'};
        $data['order_lvyoudate']=$list->{'order_lvyoudate'};
        $data['order_lvyouleibie']=Biaoqian::where('name',$list->{'order_lvyouleibie'})->first()->id;
        $data['order_beizhu']=$list->{'order_beizhu'};
        $data['order_beizhu']=$list->{'order_beizhu'};
        $statue=['1'=>'商谈','3'=>'失败','4'=>'成功'];
        $statue=array_keys($statue,$list->{'order_statue'})[0];
        $data['order_statue']=$statue;
        if($list->{'order_yuanyin'}){
            $data['order_yuanyin']=$list->{'order_yuanyin'};
        }
        $aaa=Order::updateOrCreate(array('id' =>$list->{'id'}),$data);
        if($aaa){
            return json_encode($aaa);
        }else{
            return json_encode('false');
        } 
    }
    // 调用成交单详情
    public function tianjia($id)
    {
        // 当前员工
        if(session('wechat.userid')){
            $user=\DB::table("admin_users")->where('id',session('wechat.userid'))->first();
        }else{
            return redirect(url('/'));
        }
        // 合同类型
        $type=Hetongtype::all();
        // 合同编号
        $hetong=Hetong::where('statue',2)->where('type',Hetongtype::first()->id)->where('users_id',$user->username)->get();
        // 供应商名称
        $gongyingshang=Gongyingshang::all();
        $yuangong=$user->username;
        $deal_userid=$user->id;
        $deal_customer_id=Order::where('id',$id)->first()->order_customerid;
        return view('deal.tianjia',['hetongtype'=>$type,'hetong'=>$hetong,'gongyingshang'=>$gongyingshang,'yuangong'=>$yuangong,'deal_userid'=>$deal_userid,'deal_orderid'=>$id,'deal_customer_id'=>$deal_customer_id]);
    }

    // 执行成交单添加
    public function luru(Request $request)
    {
        $list=json_decode($request['data']);
        $data=[];
        $data['deal_hetongtype']=$list->{'deal_hetongtype'};
        $data['deal_hetong']=$list->{'deal_hetong'};
        $data['deal_num']=$list->{'deal_num'};
        $data['deal_yshoukuan']=$list->{'deal_yshoukuan'};
        $data['deal_ydakuan']=$list->{'deal_ydakuan'};
        $data['deal_leibie']=$list->{'deal_leibie'};
        $data['deal_ftdate']=$list->{'deal_ftdate'};
        $data['deal_userid']=$list->{'deal_userid'};
        $data['deal_orderid']=$list->{'deal_orderid'};
        $data['deal_yuangong']=$list->{'deal_yuangong'};
        $data['deal_customer_id']=$list->{'deal_customer_id'};
        $data['deal_yglirun']=$list->{'deal_yshoukuan'}-$list->{'deal_ydakuan'};
        $data['deal_dealdate']=date("y-m-d h:i:s");
        $res=\DB::table('crm_deal')->insertGetid($data);

        if($res){
            $hetong=Hetong::where('type',Hetongtype::where('title',$list->{'deal_hetongtype'})->pluck('id')[0])->where('hetong',$list->{'deal_hetong'})->first();
            $hetong->deal_id=$res;
            $hetong->customer_id=$list->{'deal_customer_id'};
            $hetong->statue=3;
            $hetong->save();
            $order=Order::find($list->{'deal_orderid'});
            $order->order_qufen=2;
            $order->save();
            $gongyingshang=$list->{'deal_gongyingshang'};
            $gongyingshang=rtrim($gongyingshang, ",");
            $gongyingshang = explode(',',$gongyingshang);
            foreach($gongyingshang as $value){
                if($value){
                    $ll=['crm_deal_id'=>$res,'crm_supplier_id'=>$value];
                    $aa=\DB::table('crm_deal_crm_supplier')->insertGetid($ll);
                }
            }
            return json_encode($list->{'deal_orderid'});
        }else{
            return json_encode('false');
        }
    }
    // ajax获取合同类型
    public function hetongleixing()
    {
        $leixing=$_GET['hetongleixing'];
        if(session('wechat.userid')){
            $user=\DB::table("admin_users")->where('id',session('wechat.userid'))->first();
        }else{
            return redirect(url('/'));
        }
        if($leixing!=''){
            $hetongleixing=Hetongtype::where('title',$leixing)->first()->id;
            $hetong=Hetong::where('statue',2)->where('type',$hetongleixing)->where('users_id',$user->username)->pluck('hetong');
            return json_encode($hetong);
        }else{
            return json_encode('');
        }
    }
    // 调用跟单记录
    public function gendan($id)
    {
        // 当前员工
        if(session('wechat.userid')){
            $user=\DB::table("admin_users")->where('id',session('wechat.userid'))->first();
        }else{
            return redirect(url('/'));
        }
        $gendan=Gendan::where('order_id',$id)->get();
        $tai=['1'=>'未跟单','2'=>'已跟单'];
        foreach($gendan as $k=>$vo){
            $gendan[$k]['user_id']=\DB::table("admin_users")->where('id',$vo->user_id)->first()->username;
            $gendan[$k]['statue']=$tai[$vo->statue];
        }
        $user_id=$user->id;
        $order_id=$id;
        return view('order.gendan',['list'=>$gendan,'user_id'=>$user_id,'order_id'=>$order_id]);
    }
    // ajax执行跟单记录添加
    public function gendantianjia(Request $request)
    {
        $list=json_decode($request['data']);
        $data=[];
        $data['user_id']=$list->{'user_id'};
        $data['order_id']=$list->{'order_id'};
        $data['date']=$list->{'date'};
        $data['time']=date("y-m-d h:i:s");
        $aaa=Gendan::firstOrCreate($data);
        if($aaa){
            return json_encode($aaa);
        }else{
            return json_encode('false');
        }
    }
    // 调用收款页面
    public function shoukuan($id)
    {
        if(session('wechat.userid')){
            $user=\DB::table("admin_users")->where('id',session('wechat.userid'))->first();
        }else{
            return redirect(url('/'));
        }
        $zhanghu=\DB::table('crm_account')->get();
        $deal=Deal::where('id',$id)->first();
        $deal_id=$deal->id;
        $order_id=$deal->deal_orderid;
        $user_id=$user->id;
        $user_name=$user->username;
        return view('deal.shoukuan',['zhanghu'=>$zhanghu,'deal'=>$deal,'deal_id'=>$deal_id,'order_id'=>$order_id,'user_id'=>$user_id,'user_name'=>$user_name]);
    }
    // ajax获取收款账号
    public function zhanghao()
    {
        $id=$_GET['id'];
        if($id!=''){
            $data['zhanghao']=\DB::table('crm_account')->where('id',$id)->first()->account_number;
            $data['fangshi']=\DB::table('crm_account')->where('id',$id)->first()->account_type;
            return json_encode($data);
        }else{
            return json_encode('');
        }
    }
    // ajax执行收款添加
    public function shoukuantianjia(Request $request)
    {
        $data=[];
        $data['receive_dealid']=$request->receive_dealid;
        $data['receive_zhanghao']=$request->receive_zhanghao;
        $data['receive_date']=$request->receive_date;
        $data['receive_sshoukuan']=$request->receive_sshoukuan;
        $data['receive_beizhu']=$request->receive_beizhu;
        $data['receive_yuangong']=$request->receive_yuangong;
        $data['receive_userid']=$request->receive_userid;
        $data['receive_orderid']=$request->receive_orderid;
        $data['receive_zhanghu']=$request->receive_type;
        $data['receive_fangshi']=$request->receive_fangshi;
        if($request->file('photo')){
            if($request->hasFile('photo')&&$request->file('photo')->isValid()){
                $path='images/';
                $clientName=$request->file('photo')->getClientOriginalExtension();
                $clientName = time().md5('photo').'.'.$clientName;
                $request->file('photo')->move($path,$clientName);
                $data['receive_ima']=$clientName;
            }
        }
        $aaa=Receive::create($data);
        if($aaa){
            return json_encode($aaa);
        }else{
            return json_encode('false');
        }
    }
    // 调用请款页面
    public function qingkuan($id)
    {
        if(session('wechat.userid')){
            $user=\DB::table("admin_users")->where('id',session('wechat.userid'))->first();
        }else{
            return redirect(url('/'));
        }
        $deal=Deal::find($id);
        $sup=\DB::table("crm_deal_crm_supplier")->where("crm_deal_id",$deal->id)->pluck("crm_supplier_id");
        $supplier=Supplier::whereIn("id",$sup)->get();
        $deal_id=$deal->id;
        $order_id=$deal->deal_orderid;
        $user_id=$user->id;
        $user_name=$user->username;
        return view('deal.qingkuan',['supplier'=>$supplier,'deal'=>$deal,'funds_dealid'=>$deal_id,'funds_orderid'=>$order_id,'funds_userid'=>$user_id,'funds_name'=>$user_name]);
    }
    // ajax获取账户
    public function zhanghu()
    {
        $id=$_GET['id'];
        if($id!=''){
            $data=\DB::table('supplier_type')->where('supplier_id',$id)->get();
            return json_encode($data);
        }else{
            return json_encode('');
        }
    }
    // ajax获取请款账号
    public function qkzhanghao()
    {
        $id=$_GET['id'];
        if($id!=''){
            $data['zhanghao']=\DB::table('supplier_type')->where('id',$id)->first()->zhanghao;
            $data['kaihuhang']=\DB::table('supplier_type')->where('id',$id)->first()->kaihu;
            $data['funds_zhanghumingcheng']=\DB::table('supplier_type')->where('id',$id)->first()->name;
            $data['funds_type']=\DB::table('supplier_type')->where('id',$id)->first()->typename;
            return json_encode($data);
        }else{
            return json_encode('');
        }
    }
    // ajax执行请款添加
    public function qingkuantianjia(Request $request)
    {
        $data=[];
        $data['funds_gongyingshang']=$request->funds_gongyingshang;
        $data['funds_zhtype']=$request->funds_zhtype;
        $data['funds_kaihu']=$request->funds_kaihu;
        $data['funds_money']=$request->funds_money;
        $data['funds_beizhu']=$request->funds_beizhu;
        $data['funds_dealid']=$request->funds_dealid;
        $data['funds_orderid']=$request->funds_orderid;
        $data['funds_userid']=$request->funds_userid;
        $data['funds_name']=$request->funds_name;
        $data['funds_zhanghu']=$request->funds_zhanghu;
        $data['funds_zhanghumingcheng']=$request->funds_zhanghumingcheng;
        $data['funds_type']=$request->funds_type;
        $data['funds_typename']=$request->funds_typename;
        $data['funds_date']=date("y-m-d h:i:s");
        if($request->file('photo')){
            if($request->hasFile('photo')&&$request->file('photo')->isValid()){
                $path='images/';
                $clientName=$request->file('photo')->getClientOriginalExtension();
                $clientName = time().md5('photo').'.'.$clientName;
                $request->file('photo')->move($path,$clientName);
                $data['funds_ima']=$clientName;
            }
        }
        $aaa=Funds::create($data);

        if($aaa){
            $deal=Deal::find($aaa->funds_dealid);
            if($aaa->funds_money > $deal->deal_lirun){
                $fun=Funds::find($aaa->id);
                $fun->funds_dianzi=1;
                $fun->funds_dzstatue=1;
                $fun->save();
                return json_encode('dianzi');
            }else{
                $fun=Funds::find($aaa->id);
                $fun->funds_dianzi=2;
                $fun->funds_dzstatue=2;
                $fun->save();
                return json_encode('success');
            }
        }else{
            return json_encode('false');
        }
    }
    // 调用回访页面
    public function huifang($id)
    {
        if(session('wechat.userid')){
            $user=\DB::table("admin_users")->where('id',session('wechat.userid'))->first();
        }else{
            return redirect(url('/'));
        }
        $deal=Deal::find($id);
        $deal_id=$deal->id;
        $order_id=$deal->deal_orderid;
        $user_id=$user->id;
        return view('deal.huifang',['deal_id'=>$deal_id,'orderid'=>$order_id,'userid'=>$user_id]);
    }
    public function huifangtianjia(Request $request)
    {
        $list=json_decode($request['data']);
        $data=[];
        $data['title']=$list->{'title'};
        $data['date']=$list->{'date'};
        $data['deal_id']=$list->{'deal_id'};
        $data['statue']=$list->{'statue'};
        $data['userid']=$list->{'userid'};
        $data['orderid']=$list->{'orderid'};
        $res=\DB::table('crm_dchuifang')->insertGetid($data);
        if($res){
            return json_encode($list->{'orderid'});
        }else{
            return json_encode('false');
        }
    }
    public function shenqinghesuan()
    {
        $list=Deal::find($_GET['id']);
        $list->deal_jiesuanstatue=2;
        $res=$list->save();
        if($res){
            return json_encode('success');
        }else{
            return json_encode('false'); 
        }
    }
}
